﻿
Imports std

Public Module share

    <Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
    Public Function enginename() As String
        Return callingSign()
    End Function

    Private Sub initialX_separated_names(Of T As x_separated_names) _
                                        (ByRef o As T, _
                                         ByVal create As x_separated_names.createDelegate(Of T), _
                                         ByVal ParamArray s() As String)
        o = Nothing
        Dim v As vector(Of String) = Nothing
        v = New vector(Of String)()
        If Not s Is Nothing AndAlso s.Length() > 0 Then
            Dim i As Int64
            For i = 0 To s.Length() - 1
                v.push_back(s(i))
            Next
            o = create(v)
        End If
    End Sub

    Public Sub initialAliases(ByRef aliases As aliases, ByVal ParamArray s() As String)
        initialX_separated_names(aliases, AddressOf aliases.create, s)
    End Sub

    Public Sub initialMapping(Of nT As _int64) _
                             (ByRef m As parameters_mapping(Of nT), ByVal ParamArray s() As String)
        initialX_separated_names(m, AddressOf parameters_mapping(Of nT).create, s)
    End Sub

    Public Sub initialMapping(ByRef m As iParametersMapping, ByVal ParamArray s() As String)
        initialMapping(Of _maxInt64)(m, s)
    End Sub

    Public Sub initialSerialMapping(ByRef m As iParametersMapping, ByVal ParamArray s() As String)
        initialMapping(Of _0)(m, s)
    End Sub

    Public Sub initialSerialMapping1(ByRef m As iParametersMapping, ByVal ParamArray s() As String)
        initialMapping(Of _1)(m, s)
    End Sub

    Public Sub initialSerialMapping2(ByRef m As iParametersMapping, ByVal ParamArray s() As String)
        initialMapping(Of _2)(m, s)
    End Sub

    Public Function displaySource(ByVal input As String, ByVal index As Int64, _
                                  Optional ByVal len As Int64 = 32) As String
        Return ">>" + ellipsis + _
               strmid(input, index - ifoper(strlen(input) - index < len, len - (strlen(input) - index), 0), len) + _
               ellipsis + ", @" + Convert.ToString(index) + _
               ", <surrounding> {" + ellipsis + strmid(input, index - (len << 1), len << 3) + "}"
    End Function

    Public Function isMeaningfulMark(ByVal c As Char) As Boolean
        Return app._isMeaningfulMark(c) OrElse _
               bare_variable._isMeaningfulMark(c) OrElse _
               condition._isMeaningfulMark(c) OrElse _
               [function]._isMeaningfulMark(c) OrElse _
               function_call._isMeaningfulMark(c) OrElse _
               [for]._isMeaningfulMark(c) OrElse _
               foreach._isMeaningfulMark(c) OrElse _
               [while]._isMeaningfulMark(c) OrElse _
               loose_name_defination._isMeaningfulMark(c) OrElse _
               function_name_defination._isMeaningfulMark(c) OrElse _
               strong_name_defination._isMeaningfulMark(c) OrElse _
               value_name_defination._isMeaningfulMark(c) OrElse _
               operator_name_defination._isMeaningfulMark(c) OrElse _
               paragraph._isMeaningfulMark(c) OrElse _
               parameters_list._isMeaningfulMark(c) OrElse _
               sentence._isMeaningfulMark(c) OrElse _
               value._isMeaningfulMark(c) OrElse _
               value_clause._isMeaningfulMark(c) OrElse _
               value_defination._isMeaningfulMark(c) OrElse _
               variable._isMeaningfulMark(c) OrElse _
               aliases._isMeaningfulMark(c) OrElse _
               empty_parameters_mapping._isMeaningfulMark(c) OrElse _
               parameters_list2._isMeaningfulMark(c) OrElse _
               function_call2._isMeaningfulMark(c) OrElse _
               switch._isMeaningfulMark(c) OrElse _
               parameters_list3._isMeaningfulMark(c) OrElse _
               function_call3._isMeaningfulMark(c) OrElse _
               parameters_list4._isMeaningfulMark(c) OrElse _
               function_call4._isMeaningfulMark(c)
        '      comment._isMeaningfulMark(c) OrElse _
    End Function

    Public Function token(ByVal input As String, ByRef index As Int64, ByRef output As String, _
                          ByVal firstcheck As stringcheck, ByVal othercheck As stringcheck, _
                          Optional ByVal originalFirstCharCheck As charcheck = Nothing, _
                          Optional ByVal originalOtherCharCheck As charcheck = Nothing) As Boolean
        Dim expectname As String = Nothing
        If Not Global.token(input, index, output, firstcheck, othercheck, _
                            originalFirstCharCheck, originalOtherCharCheck, expectname) Then
            logparseerror("expect " + expectname, index)
            Return False
        Else
            Return True
        End If
    End Function

    Public Function token(ByVal input As String, ByRef index As Int64, ByRef output As String, _
                          ByVal firstcheck As charcheck, ByVal othercheck As charcheck) As Boolean
        Dim expectname As String = Nothing
        If Not Global.token(input, index, output, firstcheck, othercheck, expectname) Then
            logparseerror("expect " + expectname, index)
            Return False
        Else
            Return True
        End If
    End Function

    Public Delegate Function invokeMappingSizeDelegate(ByVal obj As Object) As Int64
    Public Delegate Function invokeMappingAtDelegate(ByVal index As Int64, ByRef var As bare_variable, _
                                                     ByVal obj As Object) As Boolean

    Public Function invokemapping(ByVal ctx As context, _
                                  ByVal size As invokeMappingSizeDelegate, ByVal at As invokeMappingAtDelegate, _
                                  ByVal m As iParametersMapping, _
                                  ByVal obj As Object) As Boolean
        assert(Not ctx Is Nothing, "context is nothing.")
        assert(Not m Is Nothing, "iParametersMapping is nothing.")

        Dim i As Int64
        For i = 0 To min(size(obj), m.size()) - 1
            Dim v As bare_variable = Nothing
            If Not at(i, v, obj) Then
                Return False
            Else
                ctx.insertVariable(m(i), v)
            End If
        Next

        Return True
    End Function

    Public Function string_to_bytes(ByVal s As String) As Byte()
        If Not isemptystring(s) Then
            Dim rtn() As Byte = Nothing
            rtn = encoding.utf8().GetBytes(s)
            Return rtn
        Else
            Return Nothing
        End If
    End Function
End Module
